
import React, { useEffect, useState } from 'react';
import Weight from './Weight/Weight';
import BloodSuger from './BloodSugar/BloodSuger';
import BloodPressure from './BloodPressure/BloodPressure';
import HeartRate from './HeartRate/HeartRate';
import "./HealthRecords.scss";
import { Button, Select, Modal, Form, Input, notification,Space } from 'antd';
import { useForm } from 'antd/es/form/Form';
import type { DatePickerProps } from 'antd';
import { DatePicker } from 'antd';
import { useNavigate } from 'react-router-dom';
import Index from '../../../../../api/Inedx';
import moment from 'moment';
import { useDispatch } from 'react-redux';
interface Props {
    residentId: any,
}
function HealthRecords(props: Props) {
    const [isModalOpen, setIsModalOpen] = useState(false);
    const navigate = useNavigate()
    const [form] = useForm()
    const dispacth = useDispatch()
    // --------------------------------------------------体重----------------------------------------------------
    let [weightList, setWeightList] = useState([])
    let getWeightList = async () => {
        let res = await Index.Resident.GetWeightList({ residentId: props.residentId })
        console.log(res);
        if (res.data.code === "200") {
            res.data.data.map((item: any) => {
                item.key = item.weightId
                item.datasorigin = "用户记录"
            })
            setWeightList(res.data.data)
            dispacth({ type: "SAVE_WEIGHTLIST", payload: res.data.data })
        } else {
            notification.error({ message: "查询体重失败,请去新增健康记录" })
            dispacth({ type: "SAVE_WEIGHTLIST", payload: res.data.data })
        }
    }
    let addWeight = async (time: any, data: any) => {//新增用户体重
        let res = await Index.Resident.AddWeight({ residentId: props.residentId, weightWeight: data, weightTime: time })
        console.log(res);
        if (res.data.code === "200") {
            notification.success({ message: res.data.message })
            getWeightList()
        } else {
            notification.error({ message: "新增失败!" })
        }
    }
    // ---------------------------------------------------------------------------------------------------

    // -----------------------------------------------血压------------------------------------------------
    let [bloodPressureList, setBloodPressureList] = useState([])
    let getBloodPressureList = async () => {//查询用户血压列表
        let res = await Index.Resident.GetBloodPressureList({ residentId: props.residentId })
        console.log(res);
        if (res.data.code === "200") {
            res.data.data.map((item: any) => {
                item.key = item.bpId
                item.datasorigin = "用户记录"
            })
            setBloodPressureList(res.data.data)
            dispacth({ type: "SAVE_BLOODPRESSURELIST", payload: res.data.data })
        } else {
            notification.error({ message: "查询血压失败,请去新增健康记录" })
            setIsModalOpen(true);
            dispacth({ type: "SAVE_BLOODPRESSURELIST", payload: res.data.data })
        }
    }
    let addBloodPressure = async (time: any, data: any) => {//新增用户血压信息
        let arr = data.split("/")
        let lowValue = arr[0]
        let highValue = arr[1]
        // console.log(arr);
        let res = await Index.Resident.AddBloodPressure({ residentId: props.residentId, bpLowValue: lowValue, bpHighValue: highValue, bpTime: time })
        console.log(res);
        if (res.data.code === "200") {
            notification.success({ message: res.data.message })
            getBloodPressureList()
        } else {
            notification.error({ message: "新增失败!" })
        }
    }
    // ---------------------------------------------------------------------------------------------------
    // -----------------------------------------------血糖------------------------------------------------
    let [bloodSurgeList, setBloodSurgeList] = useState([])
    let getBloodSurgeList = async () => {//查询用户血糖列表
        let res = await Index.Resident.GetBloodSurgeList({ residentId: props.residentId })
        console.log(res);
        if (res.data.code === "200") {
            res.data.data.map((item: any) => {
                item.key = item.bsId
                item.datasorigin = "用户记录"
            })
            setBloodSurgeList(res.data.data)
            dispacth({ type: "SAVE_BlOODSURGELIST", payload: res.data.data })
        } else {
            notification.error({ message: "查询血糖失败,请去新增健康记录" })
            setIsModalOpen(true);
            dispacth({ type: "SAVE_BlOODSURGELIST", payload: res.data.data })
        }
    }
    let addBloodSurge = async (time: any, data: any) => {//新增用户血糖信息
        let res = await Index.Resident.AddBloodSurge({ residentId: props.residentId, bsValue: data, bsTime: time })
        console.log(res);
        if (res.data.code === "200") {
            notification.success({ message: res.data.message })
            getBloodSurgeList()
        } else {
            notification.error({ message: "新增失败!" })
        }
    }
    // ---------------------------------------------------------------------------------------------------
    // -----------------------------------------------心率------------------------------------------------
    let [HeartRateList, setHeartRateList] = useState([])
    let getHeartRateList = async () => {//查询用户心率列表
        let res = await Index.Resident.GetHeartRateList({ residentId: props.residentId })
        console.log(res);
        if (res.data.code === "200") {
            res.data.data.map((item: any) => {
                item.key = item.hrId
                item.datasorigin = "用户记录"
            })
            setHeartRateList(res.data.data)
            dispacth({ type: "SAVE_HEARTRATELIST", payload: res.data.data })
        } else {
            notification.error({ message: "查询心率失败,请去新增健康记录" })
            setIsModalOpen(true);
            dispacth({ type: "SAVE_HEARTRATELIST", payload: res.data.data })
        }
    }
    let addHeartRate = async (time: any, data: any) => {//新增用户心率信息
        let res = await Index.Resident.AddHeartRate({ residentId: props.residentId, hrValue: data, hrTime: time })
        console.log(res);
        if (res.data.code === "200") {
            notification.success({ message: res.data.message })
            getHeartRateList()
        } else {
            notification.error({ message: "新增失败!" })
        }
    }
    // ---------------------------------------------------------------------------------------------------
    useEffect(() => {
        getWeightList()
        getBloodPressureList()
        getBloodSurgeList()
        getHeartRateList()
    }, [])

    const showModal = () => {
        setIsModalOpen(true);
    };

    const handleOk = () => {
        setIsModalOpen(false);
    };

    const handleCancel = () => {
        setIsModalOpen(false);
    };

    const onFinish = (value: any) => {//通过判断数据类型分别向不同的新增接口发请求
        // console.log(value.time);
        let a = moment(value.time._d).format("YYYY-MM-DD HH:mm:ss")//处理时间格式,通过moment库
        // console.log(a);

        setIsModalOpen(false);
        switch (value.dataType) {
            case "体重":
                addWeight(a, value.data)
                break;
            case "血糖":
                addBloodSurge(a, value.data)
                break;
            case "血压":
                addBloodPressure(a, value.data)
                break;
            case "心率":
                addHeartRate(a, value.data)
                break;
            default:
                break;
        }
    }
    let onChange: DatePickerProps['onChange'] = (date, dateString) => {
        console.log(date, dateString);
    };
    return (
        <div className='healthRecord'>
            <Weight weightList={weightList}></Weight>
            <BloodSuger></BloodSuger>
            <BloodPressure></BloodPressure>
            <HeartRate></HeartRate>
            <Space>
                <Button type='primary' onClick={showModal}>新增健康记录</Button>
                <Button type='primary' onClick={() => { navigate(-1) }}>返回</Button>
            </Space>
            <Modal title="新增" open={isModalOpen} onOk={handleOk} onCancel={handleCancel} footer={false}>
                <Form layout="vertical" onFinish={(value) => { onFinish(value) }} form={form}>
                    <Form.Item
                        label="数据类型"
                        name="dataType"
                        rules={[{ required: true, message: '请选择' }]}
                    >
                        <Select
                            placeholder="请选择"
                            style={{ width: 200 }}
                            options={[
                                { value: "体重", label: '体重' },
                                { value: "血糖", label: '血糖' },
                                { value: "血压", label: '血压' },
                                { value: "心率", label: '心率' },
                            ]}
                        />
                    </Form.Item>
                    <Form.Item
                        label="测量时间"
                        name="time"
                        rules={[{ required: true, message: '请输入时间' }]}
                    >
                        <DatePicker onChange={onChange} placeholder="请输入日期" />
                    </Form.Item>
                    <Form.Item
                        label="数值"
                        name="data"
                        rules={[{ required: true, message: '请输入数值' }]}
                    >
                        <Input placeholder="输入血压时请按照低压/高压进行输入" />
                    </Form.Item>
                    <Form.Item>
                        <Button type='primary' htmlType='submit'>保存</Button>
                    </Form.Item>
                </Form>
            </Modal>
        </div>
    );
}

export default HealthRecords;